/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ps.server;

import com.ps.server.classes.combinations;
import java.util.ArrayList;
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Ricardo
 */
public class CombinationLibraryXMLReader extends DefaultHandler {

    Vector nodos = new Vector();
    String contenido = "";
    String language = "";

    private ArrayList product_option_values;
    private ArrayList images;
    private boolean associations;
    private combinations nodoActualCombinations = new combinations();
    String resource;
    //private clsProduct nodoActual = new clsProduct();

    public CombinationLibraryXMLReader(Vector nodos) {
        this.nodos = nodos;
    }

    public CombinationLibraryXMLReader() {
        this.associations = false;
    }
    /*
     * Esta funcion el llamada cuando se produce el evento de ver una nueva
     * etiqueta
     */

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {

        if ("combination".equals(qName) && this.associations == false) {
            nodoActualCombinations = new combinations();
        }

        if ("associations".equals(qName)) {
            this.associations = true;
        }

        if ("combinations".equals(qName) && this.associations == false) {
            this.resource = qName;
        }
        if ("product_option_values".equals(qName) && this.associations == true && this.product_option_values == null && this.images == null) {
            this.product_option_values = new ArrayList();
        }
        if ("images".equals(qName) && this.associations == true && this.product_option_values != null && this.images == null) {
            this.images = new ArrayList();
        }
    }
    /*
     * Esta funcion es llamada cuando ve el contenido de una etiqueta
     */

    @Override
    public void characters(char buf[], int offset, int len)
            throws SAXException {
        contenido = new String(buf, offset, len);

    }
    /*
     * y esta al llegar al final
     */

    @Override
    public void endElement(String uri, String localName, String qName) {
        if (this.resource.equals("combinations")) {
            resourceCategories(qName);
        }
    }

    void resourceCategories(String qName) {
        if ("id".equals(qName) && this.associations == false && this.product_option_values == null && this.images == null) {
            // aqui se ya se sabe  el orden de  inicializacion de las variables.
            this.nodoActualCombinations.setId(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.product_option_values != null && this.images == null) {
            this.product_option_values.add(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.product_option_values != null && this.images != null) {
            this.images.add(contenido);
        }
//            if (this.associations == true && this.categories != null && this.products == null ) {
//                categories.add(contenido);
//            } else if (this.associations == true && this.products != null) {
//                products.add(contenido);
//            } else {
//                nodoActualCategories.setId(contenido);
//            }
//        }
        if ("id_product".equals(qName)) {
            nodoActualCombinations.setId_product(contenido);
        }
        if ("reference".equals(qName)) {
            nodoActualCombinations.setReference(contenido);
        }
        if ("supplier_reference".equals(qName)) {
            nodoActualCombinations.setSupplier_reference(contenido);
        }

        if ("location".equals(qName)) {
            nodoActualCombinations.setLocation(contenido);
        }

        if ("ean13".equals(qName)) {
            nodoActualCombinations.setEan13(contenido);
        }

        if ("upc".equals(qName)) {
            nodoActualCombinations.setUpc(contenido);
        }
        if ("wholesale_price".equals(qName)) {
            nodoActualCombinations.setWholesale_price(Double.valueOf(contenido));
        }
        if ("price".equals(qName)) {
            nodoActualCombinations.setPrice(Double.valueOf(contenido));
        }
        if ("ecotax".equals(qName)) {
            nodoActualCombinations.setEcotax(Double.valueOf(contenido));
        }
        if ("quantity".equals(qName)) {
            nodoActualCombinations.setQuantity(Integer.valueOf(contenido));
        }
        if ("weight".equals(qName)) {
            nodoActualCombinations.setWeight(contenido);
        }
        if ("default_on".equals(qName)) {
            nodoActualCombinations.setDefault_on(contenido);
        }
        /*
         * if ("product_option_values".equals(qName) && this.associations ==
         * true && this.product_option_values != null) { String[] shortString =
         * new String[this.product_option_values.size()]; for (int x = 0;
         * this.product_option_values.size() > x; x++) { shortString[x] =
         * this.product_option_values.get(x).toString(); }
         * nodoActualCombinations.setProduct_option_values(shortString); }
         */

        if ("associations".equals(qName)) {
            this.associations = false;
        }

        if ("combination".equals(qName) && this.associations == false && this.product_option_values != null && this.images != null) {

            String[] shortString = new String[this.product_option_values.size()];
            for (int x = 0; this.product_option_values.size() > x; x++) {
                shortString[x] = this.product_option_values.get(x).toString();
            }
            nodoActualCombinations.setProduct_option_values(shortString);

            shortString = new String[this.images.size()];
            for (int x = 0; this.images.size() > x; x++) {
                shortString[x] = this.images.get(x).toString();
            }
            nodoActualCombinations.setImages(shortString);

            this.nodos.add(this.nodoActualCombinations);

            this.product_option_values = null;
            this.images = null;
        }



    }

  
}